import {
	url
} from './index.js'
const http = {
	getToken() {
		let token = uni.getStorageSync('token');
		if (token) {
			return token;
		} else {
			var pages = getCurrentPages();
			const currentPage = pages[pages.length - 1];
			const path = currentPage.route;

			if (path == "pages/index/index" || //首页不验证登录
				path == 'offer/index' || //官方报价
				path == 'guarantee/list' || //质保查询
				path == 'guarantee/details' || //质保查询
				path == 'shop/register' || //加盟商注册
				path == 'login/login' //登录页不验证
			) {
				return "A";
			} else {
				uni.showToast({
					duration: 2000,
					title: '登录验证过期，请重新登录！',
					icon: 'none',
					success: () => {
						console.log('Toast shown, redirecting to login page...');
						return null;
					}
				});

				return null;
			}
		}

	},
	// 请求拦截器
	beforeRequest(options = {}) {
		return new Promise((resolve, reject) => {
			options.url = url + options.url;
			options.method = options.method || 'GET';
			options.params = options.params;
			options.tokeNull = false;

			if (this.getToken() == "A") {
				options.tokeNull = false;
				options.header = {
					"content-Type": options.method == "get" ? "" : "application/json",
					"toApplet": "to_me"
				}
			} else if (this.getToken() != null) {
				options.header = {
					"content-Type": options.method == "get" ? "" : "application/json",
					"toApplet": "to_me",
					"token": this.getToken()
				}
			} else {
				options.tokeNull = true;
			}
			resolve(options)
		})
	},
	// 相应拦截器
	handleResponse(res) {
		return new Promise((resolve, reject) => {
			if (res.code === -333) {
				uni.showToast({
					icon: 'none',
					title: '用户未登录或登录过期，请重新登录',
					duration: 3000
				});
				uni.redirectTo({
					url: '/login/login'
				})
				return;
			}
			return resolve(res);
		});
	},
	// 接口
	request(data) {

		return this.beforeRequest(data).then(opt => {
			console.log('tokeNull', opt.tokeNull)
			//alert(opt.tokeNull)
			if (opt.tokeNull) {
				return new Promise((resolve, reject) => {
					var res = {
						'code': -333,
						'data': null,
						'msg': '用户未登录或登录过期，请重新登录'
					}
					resolve(res);
				});
			} else {
				return new Promise((resolve, reject) => {
					uni.request({
						url: opt.url,
						method: opt.method,
						data: opt.params,
						header: opt.header,
						success: res => {
							resolve(res.data)
							/*
							if (res.data.code == '200'){
								resolve(res.data)	
							}
							*/
							//else http.showTost(res.data.msg || '网络错误！请重试')
						},
						fail: err => {
							//console.log(err)						
							uni.showToast({
								icon: 'none',
								duration: 5000,
								title: '后端服务器错误：' + JSON.stringify(err)
							})
							reject(err);
						},
					})
				})
			}

		}).then(res => this.handleResponse(res));
	},
	showTost(text) {
		uni.showToast({
			title: text,
			icon: "none",
			duration: 2000
		})
	},
	getUrl() {
		return url;
	},
	checkLoginToPage(url) {
		if (this.getToken() != null && this.getToken() != 'A') {
			uni.navigateTo({
				url: url
			})
		} else {
			uni.navigateTo({
				url: '/pages/sys/login'
			})
		}
	}
}
export default http